#include "batterymonitor.h"
#include "batterymonitortest.h"
#include "ui_batterymonitortest.h"
#include <qdebug.h>
#include <unistd.h>

BatteryMonitorTest::BatteryMonitorTest(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::BatteryMonitorTest)
{
    ui->setupUi(this);
    batteryMonitor = new BatteryMonitor("/tcrwork/linuxController/trunk/TcrController/data/globalConfig/batteryConfig/batteryConfig.xml");
//    timer.start(1000);
    QObject::connect(&timer,SIGNAL( timeout()), this, SLOT(on_Affirm_Button_clicked()));
    standardItemModel = new QStandardItemModel(this);

}

BatteryMonitorTest::~BatteryMonitorTest()
{
    delete ui;
}


void BatteryMonitorTest::on_Button_SetWarning_clicked()
{
    double warningVolume;
    warningVolume = ui->Edit_SetWarningVolume->text().toDouble();
    if( batteryMonitor->setWarnningBatteryLeftLightmode(warningVolume) == 1)
    {
        ui->Label_SetWarningVolume->setText("警报容量设置成功!");
        usleep(1500000);
        ui->Label_SetWarningVolume->setText("警报容量值:");
    }
    else
    {
        ui->Label_SetWarningVolume->setText("设置失败");
    }

}

void BatteryMonitorTest::on_Button_SetChargeVolume_clicked()
{
    double chargeVolume;
    chargeVolume = ui->Edit_SetChargeVolume->text().toDouble();
    if( batteryMonitor->setChargeVolume(chargeVolume) == 1)
    {
        ui->Label_SetChargeVolume->setText("充电容量设置成功!");
        usleep(1500000);
        ui->Label_SetChargeVolume->setText("充电容量:");
    }
    else
    {
        ui->Label_SetChargeVolume->setText("设置失败");
    }
}

void BatteryMonitorTest::on_SetChargeTime_Button_clicked()
{
    double chargeTime;
     chargeTime = ui->Edit_SetChargeTime->text().toDouble();
     if( batteryMonitor->setChargeTime(chargeTime) == 1)
     {
         ui->ChargeTime_Label->setText("充电容量设置成功!");
         usleep(1500000);
         ui->ChargeTime_Label->setText("充电容量:");
     }
     else
     {
         ui->ChargeTime_Label->setText("设置失败");
     }

}

//获取并显示电池组信息
void BatteryMonitorTest::on_Affirm_Button_clicked()
{
    //内部温度
    double batteryTempture;
    batteryMonitor->getBatteryTempture(batteryTempture);
    ui->Battery_Tempture_Edit->setText(QString("%1").arg(batteryTempture));

    //总电压
    double batterySumVoltage;
    batteryMonitor->getBatterySumVoltage(batterySumVoltage);
    ui->Battery_SumVoltage_Edit->setText( QString("%1").arg(batterySumVoltage));

    //电流
    double batteryCurrentCurrency;
    batteryMonitor->getBatteryCurrentCurrency(batteryCurrentCurrency);
    ui->Battery_SumVoltage_Edit->setText( QString("%1").arg(batteryCurrentCurrency));

    //相对容量比
    double batteryLeftLightmodRatio;
    batteryMonitor->getBatteryLeftLightmodRatio(batteryLeftLightmodRatio);
    ui->Battery_SumVoltage_Edit->setText( QString("%1").arg(batteryLeftLightmodRatio));

    //剩余容量
    double batteryLeftLightmodVolume;
    batteryMonitor->getBatteryLeftLightmodVolume(batteryLeftLightmodVolume);
    ui->Battery_SumVoltage_Edit->setText( QString("%1").arg(batteryLeftLightmodVolume));

    //循环次数
    double batteryCycleNumber;
    batteryMonitor->getBatteryCycleNumber(batteryCycleNumber);
    ui->Battery_SumVoltage_Edit->setText( QString("%1").arg(batteryCycleNumber));

    //设计容量
    double batteryDesignVolume;
    batteryMonitor->getBatteryDesignVolume(batteryDesignVolume);
    ui->Battery_SumVoltage_Edit->setText( QString("%1").arg(batteryDesignVolume));

    //７节电池各电压
    QVector<double>CellVoltage;
    int batteryModulId = 1;
    batteryMonitor->getBatteryOnePartVoltage(batteryModulId,CellVoltage);
    for(int i = 0; i < CellVoltage.count(); i++)
    {
        QString string = QString("%1").arg(CellVoltage.at(i));
        standarItem = new QStandardItem(string);
        standardItemModel->appendRow(standarItem);
    }
    ui->List_BatteryCell->setModel(standardItemModel);
}

void BatteryMonitorTest::on_Button_SetGoCharge_clicked()
{
    double shutdownVolume;
    shutdownVolume = ui->Edit_SetGoCharge->text().toDouble();
    if(batteryMonitor->setGoChargeBatteryLeftLightmode(shutdownVolume) == 1 )
    {
        ui->Label_SetShutdown->setText("设置成功");
        usleep(1500000);
        ui->Label_SetShutdown->setText("需要充电容量：");
    }
    else
    {
        ui->Label_SetShutdown->setText("设置失败");
    }
}
